package com.peanuts.tender.test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DES {
	
	/**
	 * DES算法
	 * CBC加密模式
	 * ***/
	public static byte[] CBCEncrypt(byte[] data, byte[] key,byte[] ivbyte) {
		try {
			Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
			DESKeySpec desKeySpec = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
	        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
	        IvParameterSpec iv = new IvParameterSpec(ivbyte);  
	        cipher.init(Cipher.ENCRYPT_MODE, secretKey,iv);
	        return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * DES 解密算法
	 * CBC 模式
	 * ***/
	public static byte[] CBCDecrypt(byte[] data,byte[] key,byte[] ivbyte){
		try {
	        Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");  
	        DESKeySpec desKeySpec = new DESKeySpec(key);  
	        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
	        IvParameterSpec iv = new IvParameterSpec(ivbyte);
	        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
	        return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 3DES 加密算法
	 * CBC
	 * **/
	public static byte[] CBC_3DesEncry(byte[] data,byte[] key,byte[] ivbyte){
		try {
			String keystr = Convert.bytesToHexString(key);
			keystr = keystr + keystr.substring(0,16);
			key = Convert.hexStringToBytes(keystr);
			
			SecretKey secretKey = new SecretKeySpec(key, "DESede");
			Cipher cipherCBC = Cipher.getInstance("DESede/CBC/NoPadding");
			IvParameterSpec iv = new IvParameterSpec(ivbyte);
			cipherCBC.init(Cipher.ENCRYPT_MODE, secretKey, iv);
			return cipherCBC.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}
	
	
	/**
	 * 3DES 解密算法
	 * CBC
	 * **/
	public static byte[] CBC_3DesDecry(byte[] data,byte[] key,byte[] ivbyte){
		try {
			String keystr = Convert.bytesToHexString(key);
			keystr = keystr + keystr.substring(0,16);
			key = Convert.hexStringToBytes(keystr);
			SecretKey secretKey = new SecretKeySpec(key, "DESede");
			Cipher cipherCBC = Cipher.getInstance("DESede/CBC/NoPadding");
			IvParameterSpec iv = new IvParameterSpec(ivbyte);
			cipherCBC.init(Cipher.DECRYPT_MODE, secretKey, iv);
			return cipherCBC.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}
	
	
	
	
	 /**
	   * DES加密源数据
	   * ECB 模式
	   * @param keybyte 密钥 8字节
	   * @param src 源数据 8字节
	   * @return byte[] 加密后的数据
	   */
	public static byte[] ECBEncrypt(byte[] data,byte[] key,byte[] ivbyte) {
		try {
			SecretKey deskey = new SecretKeySpec(key, "DES");//生成密钥
			Cipher c1 = Cipher.getInstance("DES/ECB/NoPadding");//加密
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(data);
		}catch(Exception e){
			return null;
		}
	}
	
	
	 /**
	   * DES解密源数据
	   * @param keybyte 密钥 8字节
	   * @param src 源数据 8字节
	   * @return byte[] 解密后的数据
	   */
	public static byte[] ECBDecrypt(byte[] data,byte[] key,byte[] ivbyte) {
		try {
			//生成密钥
			SecretKey deskey = new SecretKeySpec(key,"DES");
			//解密
			Cipher c1 = Cipher.getInstance("DES/ECB/NoPadding");
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(data);
		}catch (Exception e) {
			return null;
		}
	}
	
	
	/**
	   * 3Des加密数据
	   * @param dataKey 密钥 16字节
	   * @param src 待加密数据 8字节
	   * @return byte[] 加密后的数据
	   */
	  public static byte[] ECB_3DesEncry(byte[] data,byte[] key,byte[] ivbyte){
		  byte[] keyleft = new byte[8];
		  byte[] keyright = new byte[8];
		  byte[] enbyte;
		  try{
			  System.arraycopy(key, 0, keyleft, 0, 8);
			  System.arraycopy(key, 8, keyright, 0, 8);
			  //KeyL对Data进行DES加密得到Result1
			  enbyte = ECBEncrypt(data,keyleft,ivbyte);
			  //KeyR对Result1进行DES解密得到Result2
			  enbyte = ECBDecrypt(enbyte,keyright,ivbyte);
			  //KeyL对Result2进行DES加密得到Result
			  enbyte = ECBEncrypt(enbyte,keyleft,ivbyte);
			  return enbyte;
		  }catch(Exception e){
			  return null;
		  }
		  
	  }
	  
	  
	  
	  /**
	   * 3DES解密数据
	   * @param dataKey 密钥 16字节
	   * @param src 待解密数据 8字节
	   * @return byte[] 解密后的数据
	   */
	  public static byte[] ECB_3DesDecry(byte[] data,byte[] key,byte[] ivbyte){
		  byte[] keyleft = new byte[8];
		  byte[] keyright = new byte[8];
		  byte[] debyte;
		  try{
			  System.arraycopy(key, 0, keyleft, 0, 8);
			  System.arraycopy(key, 8, keyright, 0, 8);
			  //KeyL对Data进行DES解密得到Result1
			  debyte = ECBDecrypt(data,keyleft,ivbyte);
			  //KeyR对Result1进行DES加密得到Result2
			  debyte = ECBEncrypt(debyte,keyright,ivbyte);
			  //KeyL对Result2进行DES解密得到Result
			  debyte = ECBDecrypt(debyte,keyleft,ivbyte);
			  return debyte;
		  }catch(Exception e){
			  return null;
		  }
	  }
	
	
	

}
